package cn.doitedu.producer.high.tasks;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;

/**
 * @Author: 深似海
 * @Site: <a href="www.51doit.com">多易教育</a>
 * @QQ: 657270652
 * @Date: 2024/7/21
 * @Desc: 学大数据，上多易教育
 *   数据发送器
 **/
public class DataSender implements Runnable{
    ArrayBlockingQueue<String> queue;
    BufferedWriter bw;

    public DataSender(ArrayBlockingQueue<String> queue) throws IOException {
        this.queue = queue;

        Socket socket = new Socket("localhost", 9090);
        OutputStream outputStream = socket.getOutputStream();
        this.bw = new BufferedWriter(new OutputStreamWriter(outputStream));

    }


    @Override
    public void run() {

        while(true){
            String line = null;
            try {

                line = queue.take();
                System.out.println("sender取到了数据 => " + line );

                // 通过socket发送到目标系统(远端的服务)
                bw.write(line+"\n");
                bw.flush();

            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }


    }
}
